#!/usr/bin/python
import unittest
import sys
import os

# Import app modules
tests_dir = os.path.dirname(sys.argv[0])
from uya.routerconf import routes
import common

expected_routes = {
    'routing_mode': 'dynamic',
    'default_gw': '192.168.1.5',
    'default_nat': '200.0.0.1',

    'static_routes': [
        '10.15.1.0/28 10.13.1.3 0', 
        '10.15.2.0/28 10.13.2.5 0',
    ],
    
    'dynamic_static_routes': [
        '10.14.1.0/28 10.13.1.3 10',
        '0.0.0.0/0 10.13.1.4 10',
    ],
    'dynamic_password': 'somepassword',
    'dynamic_interfaces': ['ath0', 'ath5'],
    'dynamic_ospf_routerid': '0.0.1.3',
    'dynamic_ospf_routes': ['10.11.1.0/28 area 0.0.0.1',
                            '10.11.2.0/28 area 0.0.0.0'],

    'dynamic_ospf_area': ['0.0.0.1 range 10.11.17.0/20'],
    'dynamic_redistribute_static': True,
    'dynamic_redistribute_static_metric': 'metric 12 metric-type 2',
    
    'dynamic_default_information': 'normal',
    'dynamic_default_information_metric': 'metric 13 metric-type 3',                    
}

class TestRoutes(unittest.TestCase, common.BaseTestConfig):        

    root_tests_dir = tests_dir
        
    def test_network_routes(self):
        def callback(config): 
            return routes.network_routes(config["general"]["hostname"], config)
        expected_modified_filenames = [
            '/etc/network/nat-static-routes',
            '/etc/quagga/daemons',
            '/etc/quagga/zebra.conf',
            '/etc/quagga/ospfd.conf',
            ]  
        
        self.compare_filesystems(expected_modified_filenames, 
            [['/etc/init.d/networking', 'restart'], 
             ['/etc/init.d/quagga', 'restart']], callback)

    def test_network_routes_get(self):
        config = common.init_filesystems_for_get(tests_dir)
        croutes = config["routes"]
        routes.network_routes_get(croutes)
        self.assertEqual(expected_routes, croutes)

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestRoutes)
                
if __name__ == '__main__':
    unittest.main()
